package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.mall.controller.viewobject.ServiceTypeTreeVO;
import com.chinairi.mall.dao.ServiceTypeClassMapper;
import com.chinairi.mall.model.ServiceType;
import com.chinairi.mall.dao.ServiceTypeMapper;
import com.chinairi.mall.model.ServiceTypeClass;
import com.chinairi.mall.service.IServiceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.ServiceTypeMO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务类型 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-09-03
 */
@Service
public class ServiceTypeServiceImpl extends ServiceImpl<ServiceTypeMapper, ServiceType> implements IServiceTypeService {

    @Autowired
    private ServiceTypeMapper serviceTypeMapper;
    @Autowired
    private ServiceTypeClassMapper serviceTypeClassMapper;
    /**
     * @param serviceTypeName
     * @return
     */
    @Override
    public List<ServiceType> getList(String serviceTypeName) {
        QueryWrapper<ServiceType> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(serviceTypeName),"ST_SER_TYPE",serviceTypeName);
        return serviceTypeMapper.selectList(wrapper);
    }

    /**
     * 根据stSerTypeDbid查找子类
     *
     * @param dbId
     * @return
     */
    @Override
    public List<ServiceTypeMO> getListByStSerTypeDbId(String dbId) {
        return serviceTypeMapper.getListByStSerTypeDbId(dbId);
    }

    /**
     * 返回服务树
     * @return
     */
    @Override
    public ServiceTypeTreeVO getServiceTree(){
        ServiceTypeTreeVO serviceTypeTreeVo = new ServiceTypeTreeVO();
        QueryWrapper<ServiceType> wrapper = new QueryWrapper<>();
        List<ServiceType> serviceTypeList = serviceTypeMapper.selectList(wrapper);
        if (serviceTypeList.size()>0){
            Map<String, List<ServiceTypeClass>> resultMap = new HashMap<>(serviceTypeList.size());
            for (ServiceType serviceType : serviceTypeList){
                resultMap.put(serviceType.getStSerTypeDbid(),new ArrayList<>());
            }
            QueryWrapper<ServiceTypeClass> wrapper1 = new QueryWrapper<>();
            List<ServiceTypeClass> serviceTypeClassList = serviceTypeClassMapper.selectList(wrapper1);
            if (serviceTypeClassList.size()>0){
                for (ServiceTypeClass serviceTypeClass : serviceTypeClassList){
                    resultMap.get(serviceTypeClass.getStSerTypeDbid()).add(serviceTypeClass);
                }
                serviceTypeTreeVo.setResultMap(resultMap);
            }
            serviceTypeTreeVo.setServiceTypeList(serviceTypeList);
        }
     return serviceTypeTreeVo;
    }
}
